﻿// -----------------------------------------------------------------------
// <copyright file="Utilities.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

using System.Globalization;
using Microsoft.SharePoint;

namespace LifeInSharePoint.PromotedLinks.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Utilities
    {

        /// <summary>
        /// Safelly gets the string value of a SPListItem colum.
        /// </summary>
        /// <param name="item">
        /// </param>
        /// <param name="fieldName">
        /// </param>
        /// <returns>
        /// The get column string value.
        /// </returns>
        public static string GetColumnStringValue(SPListItem item, string fieldName)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            EnsureFieldExists(item, fieldName);

            object obj = item[fieldName];

            if (obj != null)
            {
                return obj.ToString();
            }

            return String.Empty;
        }

        /// <summary>
        /// Safelly gets the object value of a SPListItem colum.
        /// </summary>
        /// <param name="item">
        /// </param>
        /// <param name="fieldName">
        /// </param>
        /// <returns>
        /// The get column value.
        /// </returns>
        public static object GetColumnValue(SPListItem item, string fieldName)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            EnsureFieldExists(item, fieldName);

            return item[fieldName];
        }


        /// <summary>
        /// The ensure field exists.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <param name="fieldName">
        /// The field name.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        public static void EnsureFieldExists(SPListItem item, string fieldName)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            if (!item.Fields.ContainsField(fieldName))
            {
                string errorMsg = String.Format(
                    CultureInfo.CurrentCulture,
                    "List {0} does not contain field named {1}",
                    item.ParentList.Title,
                    fieldName);
                Console.WriteLine(errorMsg);
                throw new ArgumentOutOfRangeException(errorMsg);
            }
        }


        /// <summary>
        /// Safelly gets the int value of a SPListItem colum.
        /// </summary>
        /// <param name="item">
        /// </param>
        /// <param name="fieldName">
        /// </param>
        /// <returns>
        /// The get column int value.
        /// </returns>
        public static int GetColumnIntValue(SPListItem item, string fieldName)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            EnsureFieldExists(item, fieldName);

            object obj = item[fieldName];

            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }

            return 0;
        }

    }
}
